<template>
  <div class="my-container">
    <div v-if="user" class="header login">
      <div class="base">
        <div class="left">
          <van-image
            fit="cover"
            :src="userInfo.photo"
            class="avatar"
            round
          />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <van-button type="default" size="mini" round to="/profile">编辑资料</van-button>
      </div>

      <div class="data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>

    <div v-else class="header no-login">
      <div class="mobile-login-warp" @click="$router.push('/login')">
        <img class="mobile" src="~@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>
 
    <van-grid class="nav-grid" :column-num="2" clickable >
      <van-grid-item text="收藏">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span class="text" slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item text="文字">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span class="text" slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell 
      v-if="user" 
      class="logout-cell" 
      title="退出登录" 
      center
      @click="logout" 
    />
  </div>
</template>

<script>
import {mapState} from 'vuex'
import {removeItem} from '@/utils/storage'
import {getUserInfo} from '@/api/user'

export default {
  name: 'my',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    if(this.user) {
      this.getUserInfoFn()
    }
  },
  methods: {
    logout() {
      this.$dialog.confirm({
        title: '确认退出吗？'
      }).then(() => {
        this.$store.commit('setUser', null)
        removeItem('token_user')
      }).catch(() => {
        console.log('取消执行这里')
      })
     
    },
    async getUserInfoFn() {
      try {
        const {data} = await getUserInfo()

        this.userInfo = data.data
      } catch(err) {
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header{
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  .no-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .mobile-login-warp {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    .mobile {
      width: 137px;
      height: 137px;
      margin-bottom: 15px;
    }
    .text{
      color: #fff;
      font-size: 30px;
    }
  }

  .base {
    display: flex;
    box-sizing: border-box;
    padding: 76px 32px 23px;
    justify-content: space-between;
    align-items: center;
    height: 231px;
    .avatar{
      width: 132px;
      height: 132px;
      border: 3px solid #fff;
    }
    .left{
      display: flex;
      align-items: center;
      .name{
        color:#fff;
        margin-left: 15px;
        font-size: 30px;
      }
    }
  }
  .data{
    height: 130px;
    display: flex;
    justify-content: space-around;
    .data-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      color: #fff;
      span:nth-child(1) {
        font-size: 36px;
      }
      span:nth-child(2) {
        font-size: 24px;
      }
    }
  }
  .nav-grid {
    background: #fff;
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #EB5253;
    }
    .toutiao-lishi {
      color: #FF9D1D;
    }
    .text {
      color: #000;
      margin-top: 8px;
      font-size: 28px;
    }
  }
  .logout-cell{
    height: 100px;
    text-align: center;
    color: #EB5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
